//
//  ViewController.m
//  防映客TableView滚动
//
//  Created by Apple on 2017/11/17.
//  Copyright © 2017年 郭凯. All rights reserved.
//

#define headViewHeight 200
#define PageMenuH 40
#import "ViewController.h"
#import "MyTableView.h"
#import "SPPageMenu.h"
#import "FirstViewController.h"
#import "TwoViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,SPPageMenuDelegate>
@property (strong, nonatomic) IBOutlet MyTableView *mainTableView;
@property (nonatomic, strong) SPPageMenu *pageMenu;
@property (nonatomic,strong) UIScrollView* scrollView;
@property (nonatomic,strong) UIScrollView* childVCScrollView;
@end

@implementation ViewController

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat height = [UIScreen mainScreen].bounds.size.height;
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, PageMenuH, width, height-PageMenuH-64);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(width*2, 0);
        _scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    }
    return _scrollView;
}

- (SPPageMenu *)pageMenu {
    if (!_pageMenu) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        _pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, width, PageMenuH) trackerStyle:SPPageMenuTrackerStyleLineLongerThanItem];
        [_pageMenu setItems:@[@"第一页",@"第二页"] selectedItemIndex:0];
        _pageMenu.delegate = self;
        _pageMenu.itemTitleFont = [UIFont systemFontOfSize:16];
        _pageMenu.selectedItemTitleColor = [UIColor orangeColor];
        _pageMenu.unSelectedItemTitleColor = [UIColor blackColor];
        _pageMenu.tracker.backgroundColor = [UIColor orangeColor];
        _pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
        _pageMenu.bridgeScrollView = self.scrollView;
    }
    return _pageMenu;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"仿映客";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, headViewHeight)];
    headView.backgroundColor = [UIColor orangeColor];
    self.mainTableView.tableHeaderView = headView;
    
    FirstViewController *firstVc = [[FirstViewController alloc]init];
    TwoViewController *twoVc = [[TwoViewController alloc]init];
    [self addChildViewController:firstVc];
    [self addChildViewController:twoVc];
    firstVc.view.frame = self.scrollView.bounds;
    twoVc.view.frame = CGRectMake( self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    [self.scrollView addSubview:firstVc.view];
    [self.scrollView addSubview:twoVc.view];
    
    
    // 监听子控制器发出的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subTableViewDidScroll:) name:@"SubTableViewDidScroll" object:nil];
}

//当mainScrollerview滚动的时候
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  
    if (self.mainTableView == scrollView) {
        if (self.childVCScrollView && _childVCScrollView.contentOffset.y > 0) {
            self.mainTableView.contentOffset = CGPointMake(0, headViewHeight);
        }
        CGFloat offSetY = scrollView.contentOffset.y;
        if (offSetY < headViewHeight) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"headerViewToTop" object:nil];
        }
    }
}
//当子tableView滚动的时候
- (void)subTableViewDidScroll:(NSNotification *)noti {
    UIScrollView *scrollView = noti.object;
    self.childVCScrollView = scrollView;
    if (self.mainTableView.contentOffset.y < headViewHeight) {
        scrollView.contentOffset = CGPointZero;
        scrollView.showsVerticalScrollIndicator = NO;
        
    }else{
        scrollView.showsVerticalScrollIndicator = YES;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor blueColor];
    [cell.contentView addSubview:self.pageMenu];
    [cell.contentView addSubview:self.scrollView];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.bounds.size.height-64;
}


#pragma mark - SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index{
    NSLog(@"INDEX===%ld",index);
}
@end
